package com.ruoyi.know.controller;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.know.domain.UserFollow;
import com.ruoyi.know.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/know/follow")
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    /**
     * 关注用户
     * @param userFollow 关注关系实体
     * @return 插入成功的记录数
     */
    @PostMapping
    public int followUser(@RequestBody UserFollow userFollow) {
        return userFollowService.followUser(userFollow);
    }

    /**
     * 取消关注用户
     * @param followerId 关注者 ID
     * @param followedId 被关注者 ID
     * @return 删除成功的记录数
     */
    @DeleteMapping("/{followerId}/{followedId}")
    public int unfollowUser(@PathVariable int followerId, @PathVariable int followedId) {
        return userFollowService.unfollowUser(followerId, followedId);
    }

    /**
     * 获取用户的关注列表
     * @param followerId 关注者 ID
     * @return 关注列表
     */
    @GetMapping("/getFollowedUsers")
    public List<SysUser> getFollowedUsers(@RequestParam int followerId,@RequestParam(required = false) String nickName) {
        if(StringUtils.isEmpty(nickName)){
            nickName = null;
        }
        return userFollowService.getFollowedUsers(followerId,nickName);
    }

    /**
     * 获取关注该用户的其他用户列表
     * @param followedId  关注者 ID
     * @param nickName   昵称
     * @return 被关注的用户列表
     */
    @GetMapping("/getNewFollowers")
    public List<SysUser> getNewFollowers(@RequestParam int followedId,@RequestParam(required = false) String nickName) {
        if(StringUtils.isEmpty(nickName)){
            nickName = null;
        }
        return userFollowService.getNewFollowers(followedId,nickName);
    }
}
